package com.igeekhome.ccs.biz.impl;

import com.igeekhome.ccs.biz.ICommonWordsBiz;
import com.igeekhome.ccs.entity.CommonWords;
import com.igeekhome.ccs.entity.CustomerServiceGroup;
import com.igeekhome.ccs.mapper.CommonWordsCategoryMapper;
import com.igeekhome.ccs.mapper.CommonWordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CommonWordsbizImpl implements ICommonWordsBiz {
//👇zyh--------------------------------------------------------
     @Autowired
    CommonWordsMapper commonWordsMapper;

     @Autowired
    CommonWordsCategoryMapper commonWordsCategoryMapper;


    //模糊匹配公共库
    @Override
    public List<CommonWords> matchPublicCmWithString(String str) {
        List<CommonWords> totalList = commonWordsMapper.queryPublicLib();
        List<CommonWords> resultList = new ArrayList<CommonWords>();

        str=str.trim();
        for(int i = 0 ; i<totalList.size(); i++) {
            //模糊匹配、大小写均可
            Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(totalList.get(i).getContent());
            if (matcher.find() ) {
                resultList.add(totalList.get(i));
            }
        }
        return resultList;
    }
    //模糊匹配个人库
    @Override
    public List<CommonWords> matchPrivateCmWithString(String str, int csId) {
        List<CommonWords> totalList = commonWordsMapper.queryPrivateLib(csId);
        List<CommonWords> resultList = new ArrayList<CommonWords>();

        str=str.trim();
        for(int i = 0 ; i<totalList.size(); i++) {
            //模糊匹配、大小写均可
            Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(totalList.get(i).getContent());
            if (matcher.find() ) {
                resultList.add(totalList.get(i));
            }
        }
        return resultList;
    }

    //通过常用语ID拿到分类名
    @Override
    public String queryCategoryNameByCmId(int cmId) {
        String categoryId = commonWordsMapper.getCategoryIdByCmId(cmId);
        return commonWordsCategoryMapper.getCategoryNameById(categoryId);
    }

    @Override
    public int createCw(CommonWords commonWords) {
        commonWordsMapper.createCw(commonWords);
        return 0;
    }

//👆zyh----------------------------------------------------------
}
